﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingClientsWebsiteManager.Admin
{
    public partial class ImeiManager : System.Web.UI.Page
    {

        private void GetGroupsByUser(string Username)
        {
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.UsersByGroups> usersByGroupsEntities = GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.GetByUsername(Username);

            GPSTrackingServerDB.Data.DataRepository.UsersByGroupsProvider.DeepLoad(usersByGroupsEntities, true, GPSTrackingServerDB.Data.DeepLoadType.IncludeChildren, typeof(GPSTrackingServerDB.Entities.Groups));

            //Parse
            GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Groups> groupsEntities = new GPSTrackingServerDB.Entities.TList<GPSTrackingServerDB.Entities.Groups>();
            foreach (GPSTrackingServerDB.Entities.UsersByGroups entity in usersByGroupsEntities)
            {
                groupsEntities.Add(entity.GroupIdSource);
            }

            GroupGrid.DataSource = groupsEntities;
            GroupGrid.DataBind();
        }


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ImeiGroup_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (GroupGrid.SelectedItems.Count == 0)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowError", "show_Info('" +"Debes seleccionar un grupo"+ "','Grupo ...');", true);
                e.Canceled = true;
                return;
            }

            if (ImeiGroup.SelectedItems.Count > 0)
            {                
                GPSTrackingServerDB.Data.TransactionManager transactionManager = GPSTrackingServerDB.Data.DataRepository.Provider.CreateTransaction();
                transactionManager.BeginTransaction();

                int RowIndex = 1;
                foreach (Telerik.Web.UI.GridDataItem item in ImeiGroup.SelectedItems)
                {
                    //Imei
                    GPSTrackingServerDB.Entities.Imei imeiEntity = GPSTrackingServerDB.Data.DataRepository.ImeiProvider.GetByImei(item.Cells[3].Text);
                    if (imeiEntity == null)
                    {
                        imeiEntity = new GPSTrackingServerDB.Entities.Imei();
                        imeiEntity.Imei = item.Cells[3].Text;
                        imeiEntity.Name = "GPS_" + RowIndex.ToString();
                        imeiEntity.Description = "GPS_" + RowIndex.ToString();
                        imeiEntity.SimNumber = 0;

                        imeiEntity.ImeiTypeId = Convert.ToInt32(((Telerik.Web.UI.RadComboBox)(item.FindControl("RadComboBox1"))).SelectedValue);
                        imeiEntity.GpsModelId = Convert.ToInt16(((Telerik.Web.UI.RadComboBox)(item.FindControl("RadComboBox2"))).SelectedValue);


                        GPSTrackingServerDB.Data.DataRepository.ImeiProvider.Insert(transactionManager, imeiEntity);
                    }
                    //Group by imei
                    GPSTrackingServerDB.Entities.GroupsByImei groupsByImeiEntity = new GPSTrackingServerDB.Entities.GroupsByImei();
                    groupsByImeiEntity.Imei = item.Cells[3].Text;
                    groupsByImeiEntity.GroupId = Convert.ToInt32(GroupGrid.SelectedValue);

                    GPSTrackingServerDB.Data.DataRepository.GroupsByImeiProvider.Insert(transactionManager, groupsByImeiEntity);

                    RowIndex++;
                }

                transactionManager.Commit();               

                RadNotification1.Show();

                //Refresh
                GroupGrid.DataSource = new System.Data.DataTable();

                ImeiGroup.DataSource = new System.Data.DataTable();

            }

            e.Canceled = true;

        }

        protected void UserGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            UserGrid.DataSource = GPSTrackingServerDB.Data.DataRepository.UsersProvider.GetAll();
        }

        protected void UserGrid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "UserEdit")
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable entity = new Hashtable();
                item.ExtractValues(entity);

                string  Username = entity[GPSTrackingServerDB.Entities.UsersColumn.Username.ToString()].ToString();

                GetGroupsByUser(Username);
            }
        }

        protected void ImeiGroup_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            ImeiGroup.DataSource = GPSTrackingServerDBCustom.Data.DataRepository.ImeiProvider.GetAllImeiWithinUser();
        }

        protected void GroupGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            GroupGrid.DataSource = new System.Data.DataTable();
        }

        protected void ImeiGroup_ItemDataBound(object sender, GridItemEventArgs e)
        {
            try
            {
                if (e.Item is GridDataItem)
                {
                    GridDataItem gridDataItem = e.Item as GridDataItem;
                    RadComboBox RadComboBox1 = gridDataItem.FindControl("RadComboBox1") as RadComboBox;

                    RadComboBox1.DataSource = GPSTrackingServerDB.Data.DataRepository.ImeiTypeProvider.GetAll();
                    RadComboBox1.DataTextField = "Description";
                    RadComboBox1.DataValueField = "ImeiTypeID";
                    RadComboBox1.DataBind();

                    RadComboBox1.SelectedValue = "0";
                    
                    RadComboBox RadComboBox2 = gridDataItem.FindControl("RadComboBox2") as RadComboBox;

                    RadComboBox2.DataSource = GPSTrackingServerDB.Data.DataRepository.GpsModelsProvider.GetAll();
                    RadComboBox2.DataTextField = "Name";
                    RadComboBox2.DataValueField = "GPSModelID";
                    RadComboBox2.DataBind();

                    RadComboBox2.SelectedValue = "0";

                }
            }
            catch (Exception ex)
            {

                GPSTrackingLogCustom.WebsiteLog.Error(ex.Message);
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ShowErrorCatch", "show_miniError('" + ex.Message + "','Grupos error ...');", true);
            }  
        }
    }
}